﻿Imports ArtNoi

Partial Class UserControls_CadastroPessoa
    Inherits AppBaseUserControl

#Region "Atributos da pagina"

    Public Property ListaTelefonesPessoa() As List(Of TelefoneAux)
        Get
            Return TryCast(ViewState("_ListaTelefonesPessoa"), List(Of TelefoneAux))
        End Get
        Set(ByVal value As List(Of TelefoneAux))
            ViewState("_ListaTelefonesPessoa") = value
        End Set
    End Property

    Public Property ListaEmailPessoa() As List(Of EmailAux)
        Get
            Return TryCast(ViewState("_ListaEmailPessoa"), List(Of EmailAux))
        End Get
        Set(ByVal value As List(Of EmailAux))
            ViewState("_ListaEmailPessoa") = value
        End Set
    End Property

#End Region

#Region "Delegates"

    Protected Delegate Sub PageDelegate()

#End Region

#Region "Classes Injetadas"

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

        End If

    End Sub

#Region "Funcoes de Databind"

    Public Sub DatabindComponente()



    End Sub

#End Region

#Region "Operacoes do Usuario"

    Protected Sub btnInserirTelefone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTelefone.Click

        Dim _listaValidacoes As New List(Of Boolean)

        _listaValidacoes.Add(Not String.IsNullOrEmpty(txtTelefone.Text))
        _listaValidacoes.Add(Not String.IsNullOrEmpty(txtDDD.Text))
        _listaValidacoes.Add(Not String.IsNullOrEmpty(ddlTipoTelefone.SelectedValue))

        If Not _listaValidacoes.Contains(False) Then

            Dim _telefone As New TelefoneAux
            _telefone.Numero = txtTelefone.Text
            _telefone.DDD = txtDDD.Text
            _telefone.Tipo = ddlTipoTelefone.SelectedValue

            If Me.ListaTelefonesPessoa Is Nothing Then Me.ListaTelefonesPessoa = New List(Of TelefoneAux)

            Me.ListaTelefonesPessoa.Add(_telefone)

            gdTelefone.DataSource = Me.ListaTelefonesPessoa
            gdTelefone.DataBind()

        Else
            MyBase.ShowMessage("É necessário preencher os campos para incluir o telefone.")
        End If

    End Sub

    Protected Sub btnInserirEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEmail.Click

        Dim _listaValidacoes As New List(Of Boolean)

        _listaValidacoes.Add(Not String.IsNullOrEmpty(txtEmail.Text))

        If Not _listaValidacoes.Contains(False) Then
            Dim _email As New EmailAux
            _email.Email = txtEmail.Text

            If Me.ListaEmailPessoa Is Nothing Then Me.ListaEmailPessoa = New List(Of EmailAux)

            ListaEmailPessoa.Add(_email)

            gdEmail.DataSource = ListaEmailPessoa
            gdEmail.DataBind()
        Else
            MyBase.ShowMessage("É necessário preencher os campos para incluir o email.")
        End If

    End Sub

    Protected Sub btnSalvar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSalvar.Click
        Dim _funcionario As New Funcionario
        Dim _endereco As New Endereco
        Dim _listaTelefones As New List(Of Telefone)
        Dim _listaEmails As New List(Of Email)

        _funcionario.Nome = txtNome.Text
        _funcionario.CPF = txtCPF.Text
        _funcionario.CTPS = txtCTPS.Text
        _funcionario.DataNascimento = txtDataNascimento.Text
        _funcionario.RG = txtRG.Text

        _endereco.Logradouro = txtEndereco.Text
        _funcionario.Enderecos = New List(Of Endereco)
        _funcionario.Enderecos.Add(_endereco)

        _funcionario.Contatos = New List(Of Contato)

        For Each _email In ListaEmailPessoa
            Dim _emailParseado As New Email
            _emailParseado = _email.ParseToEmail()
            _funcionario.Contatos.Add(_emailParseado)
        Next

        For Each _tels In ListaTelefonesPessoa
            Dim _telefoneParseado As New Telefone
            _telefoneParseado = _tels.ParseToTelefone()
            _funcionario.Contatos.Add(_telefoneParseado)
        Next

        _funcionario.Salvar()

    End Sub

    Protected Sub btnLimparEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLimparEmail.Click
        Me.ListaEmailPessoa.Clear()
        gdEmail.DataSource = Nothing
        gdEmail.DataBind()
    End Sub

    Protected Sub btnLimparTelefone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLimparTelefone.Click
        Me.ListaTelefonesPessoa.Clear()
        gdTelefone.DataSource = Nothing
        gdTelefone.DataBind()
    End Sub

#End Region

#Region "Classes Auxiliares"

    <Serializable()> _
    Public Class TelefoneAux

        Private _ddd As String
        Public Overridable Property DDD() As String
            Get
                Return _ddd
            End Get
            Set(ByVal value As String)
                _ddd = value
            End Set
        End Property

        Private _numero As String
        Public Overridable Property Numero() As String
            Get
                Return _numero
            End Get
            Set(ByVal value As String)
                _numero = value
            End Set
        End Property

        Private _tipoTelefone As TipoTelefone
        Public Overridable Property Tipo() As TipoTelefone
            Get
                Return _tipoTelefone
            End Get
            Set(ByVal value As TipoTelefone)
                _tipoTelefone = value
            End Set
        End Property

        Public Function ParseToTelefone() As Telefone
            Dim _t As Telefone
            _t = New Telefone
            With _t
                .Numero = Me.Numero
                .DDD = Me.DDD
                .Tipo = Me.Tipo
            End With

            Return _t

        End Function


        
    End Class

    <Serializable()> _
    Public Class EmailAux

        Private _email As String
        Public Overridable Property Email() As String
            Get
                Return _email
            End Get
            Set(ByVal value As String)
                _email = value
            End Set
        End Property

        Public Function ParseToEmail() As Email
            Dim _t As Email
            _t = New Email
            With _t
                .Endereco = Me.Email
            End With

            Return _t

        End Function

    End Class

#End Region

    Protected Sub gdTelefone_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gdTelefone.RowDataBound
        If Not e.Row.RowType = DataControlRowType.DataRow Then Exit Sub

        Dim _telefoneAux As TelefoneAux = CType(e.Row.DataItem, TelefoneAux)

        Dim _litTipo As Literal = e.Row.FindControl("litTipo")
        Dim _litNumero As Literal = e.Row.FindControl("litNumero")

        With _telefoneAux
            _litTipo.Text = .Tipo
            _litNumero.Text = .Numero
        End With

    End Sub

 
End Class
